package com;

import java.util.ArrayList;
import com.android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class config_activity extends Activity {	
	Button btnSave;
	Button btnExit;
	
	EditText txtUserName;
	EditText txtPassword;
	EditText txtSendto;
	EditText txtUssdCode;
	EditText txtSubject;
	
	CheckBox chkMobile3G;
	CheckBox chkWifi;
	
	ArrayList<String> config;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config_activity);
		setTitle(getString(R.string.title));
				
		btnExit=(Button)findViewById(R.id.btnExit);
		btnExit.setOnClickListener(btnExit_OnClickListenner);
		btnSave=(Button)findViewById(R.id.btnSave);
		btnSave.setOnClickListener(btnSave_OnClickListenner);
		
		txtSendto=(EditText)findViewById(R.id.txtSendTo);		
		txtUserName=(EditText)findViewById(R.id.txtUserName);		
		txtPassword=(EditText)findViewById(R.id.txtPassword);		
		txtUssdCode=(EditText)findViewById(R.id.txtUSSDCode);		
		txtSubject=(EditText)findViewById(R.id.txtSubject);
		
		chkMobile3G=(CheckBox)findViewById(R.id.chkMobile3G);
		chkWifi=(CheckBox)findViewById(R.id.chkWifi);		

		//Load config
		config=common.GetConfig(getApplicationContext());
		if (config.size()>=7){
			txtUserName.setText(config.get(0));
			txtPassword.setText(config.get(1));
			txtSendto.setText(config.get(2));
			txtSubject.setText(config.get(3));
			txtUssdCode.setText(config.get(4));
			boolean check3G=config.get(5).equals("true")?true:false;
			boolean checkWifi=config.get(6).equals("true")?true:false;
			chkMobile3G.setChecked(check3G);
			chkWifi.setChecked(checkWifi);
			
		}
		
		txtUssdCode.addTextChangedListener(watcherTextChange);	
		txtPassword.addTextChangedListener(watcherTextChange);
		txtUserName.addTextChangedListener(watcherTextChange);
		txtSendto.addTextChangedListener(watcherTextChange);
		chkMobile3G.setOnCheckedChangeListener(onCheckedListenner);
		chkWifi.setOnCheckedChangeListener(onCheckedListenner);
	}

	OnCheckedChangeListener onCheckedListenner=new  OnCheckedChangeListener(
			) {
		
		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			btnSave.setEnabled(true);
		}
	};
	
	TextWatcher watcherTextChange=new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
						
		}		
		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {

		}		
		@Override
		public void afterTextChanged(Editable arg0) {
			btnSave.setEnabled(true);
		}
	};
	
	OnClickListener btnExit_OnClickListenner=new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(btnSave.isEnabled()){
			 AlertDialog.Builder d=new AlertDialog.Builder(arg0.getContext());
			 d.setTitle(getString(R.string.confirm));
			 d.setIcon(0);
			 d.setMessage(getString(R.string.msg001));
			 d.setNegativeButton(getString(R.string.no), null);
			 d.setNeutralButton(getString(R.string.yes), listener);
			 d.show();		
			 }else{
				 System.exit(0);
			 }
		}
	};
	
	DialogInterface.OnClickListener listener =new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			System.exit(0);
		}
	};
	
	OnClickListener btnSave_OnClickListenner=new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if (checkNull()){
				Toast.makeText(arg0.getContext().getApplicationContext(), 
						getString(R.string.msg002), Toast.LENGTH_SHORT).show();
				btnSave.setEnabled(false);
				config.clear();
				config.add(txtUserName.getText().toString());
				config.add(txtPassword.getText().toString());
				config.add(txtSendto.getText().toString());
				config.add(txtSubject.getText().toString());
				config.add(txtUssdCode.getText().toString());
				config.add(String.valueOf(chkMobile3G.isChecked()));
				config.add(String.valueOf(chkWifi.isChecked()));
				common.WriteConfig(getApplicationContext(), config);
			}
		}
	};
	
	public boolean checkNull(){
		if (txtUserName.getText().length()==0){
			Toast.makeText(getApplicationContext(), 
					getString(R.string.msg003, 
							getString(R.string.user_name)), 
					Toast.LENGTH_SHORT).show();
			txtUserName.requestFocus();
			return false;}
		if (txtPassword.getText().length()==0){
			Toast.makeText(getApplicationContext(), 
					getString(R.string.msg003, 
							getString(R.string.password)), 
					Toast.LENGTH_SHORT).show();
			txtPassword.requestFocus();
			return false;}
		if (txtSendto.getText().length()==0){
			Toast.makeText(getApplicationContext(), 
					getString(R.string.msg003, 
							getString(R.string.send_to)), 
					Toast.LENGTH_SHORT).show();
			txtSendto.requestFocus();
			return false;}
		if (txtUssdCode.getText().length()==0){
			Toast.makeText(getApplicationContext(), 
					getString(R.string.msg003, 
							getString(R.string.ussd_code)), 
					Toast.LENGTH_SHORT).show();
			txtUssdCode.requestFocus();
			return false;}
		return true;
	}
}